﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OutboundValidationBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICustomerDA customerDA = DataAccess.CreateCustomerDA();
        private List<string> dataBaseID;
        private static readonly IOutboundValidationDA OutboundValidationDA = DataAccess.CreateOutboundValidationDA();
        private static readonly IOutboundValidationDetailDA OutboundValidationDetailDA = DataAccess.CreateOutboundValidationDetailDA();
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();

        public OutboundValidationBLL()
        {
            this.pubParasInfo = null;
        }

        public OutboundValidationBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OutboundValidationBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                int i;
                for (i = 0; i < IDS.Count; i++)
                {
                    OutboundValidationDetailDA.DeleteOutboundValidationDetail(dataBase, tran, IDS[i], this.dataBaseID[0]);
                }
                if (OutboundValidationDA.DeleteOutboundValidation(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < IDS.Count; i++)
                    {
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return OutboundValidationDA.GetAllOutboundValidation(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            OutboundValidationInfo outboundValidationInfo = OutboundValidationDA.GetOutboundValidationByID(id, this.dataBaseID[0]);
            if (outboundValidationInfo != null)
            {
                outboundValidationInfo.OutboundValidationDetailInfos = OutboundValidationDetailDA.GetOutboundValidationDetailByList(id, this.dataBaseID[0]);
            }
            return outboundValidationInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            OutboundValidationQueryEntity outboundValidationQueryEntity = obj as OutboundValidationQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(outboundValidationQueryEntity.ValidationID))
            {
                sqlWhere.Add(" VALIDATION_ID " + outboundValidationQueryEntity.ValidationID);
            }
            if (!string.IsNullOrEmpty(outboundValidationQueryEntity.Descr))
            {
                sqlWhere.Add(" DESCR " + outboundValidationQueryEntity.Descr);
            }
            if (!string.IsNullOrEmpty(outboundValidationQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + outboundValidationQueryEntity.IsActive);
            }
            return OutboundValidationDA.GetOutboundValidationByQueryList(sqlWhere, outboundValidationQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            OutboundValidationInfo outboundValidationInfo = obj as OutboundValidationInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strOutboundValidationID = string.Empty;
            try
            {
                if (!DataAccess.CreateOwnerDA().CheckTradePartnerIDUnique(outboundValidationInfo.OwnerID, whLoginID))
                {
                    throw new Exception(outboundValidationInfo.OwnerID + "货主不存在!");
                }
                if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, outboundValidationInfo.OwnerID))
                {
                    throw new Exception("没有" + outboundValidationInfo.OwnerID + "货主权限!");
                }
                if (!skuDA.CheckSkuIDUnique(dataBase, tran, outboundValidationInfo.OwnerID, outboundValidationInfo.SkuID, this.dataBaseID[0]))
                {
                    throw new Exception(outboundValidationInfo.SkuID + "货品不存在!");
                }
                if (!customerDA.CheckCustomerIDUnique(outboundValidationInfo.ConsigneeID, whLoginID))
                {
                    throw new Exception(outboundValidationInfo.ConsigneeID + "客户不存在!");
                }
                if (!customerDA.CheckAuthorizationCustomerIDUserID(outboundValidationInfo.ConsigneeID, whLoginID, this.pubParasInfo))
                {
                    throw new Exception("没有" + outboundValidationInfo.ConsigneeID + "客户权限!");
                }
                outboundValidationInfo.UpdatedBy = outboundValidationInfo.CreatedBy;
                outboundValidationInfo.CreatedDate = DateTime.Now;
                outboundValidationInfo.UpdatedDate = DateTime.Now;
                if (OutboundValidationDA.InsertOutboundValidation(dataBase, tran, outboundValidationInfo, whLoginID) > 0)
                {
                    strOutboundValidationID = outboundValidationInfo.ValidationID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "ACCESSORIAL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strOutboundValidationID;
        }

        public string Update(object obj)
        {
            OutboundValidationInfo outboundValidationInfo = obj as OutboundValidationInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strOutboundValidationID = string.Empty;
            try
            {
                if (!ownerDA.CheckTradePartnerIDUnique(outboundValidationInfo.OwnerID, whLoginID))
                {
                    throw new Exception(outboundValidationInfo.OwnerID + "货主不存在!");
                }
                if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, outboundValidationInfo.OwnerID))
                {
                    throw new Exception("没有" + outboundValidationInfo.OwnerID + "货主权限!");
                }
                if (!skuDA.CheckSkuIDUnique(dataBase, tran, outboundValidationInfo.OwnerID, outboundValidationInfo.SkuID, this.dataBaseID[0]))
                {
                    throw new Exception(outboundValidationInfo.SkuID + "货品不存在!");
                }
                if (!customerDA.CheckCustomerIDUnique(outboundValidationInfo.ConsigneeID, whLoginID))
                {
                    throw new Exception(outboundValidationInfo.ConsigneeID + "客户不存在!");
                }
                if (!customerDA.CheckAuthorizationCustomerIDUserID(outboundValidationInfo.ConsigneeID, whLoginID, this.pubParasInfo))
                {
                    throw new Exception("没有" + outboundValidationInfo.ConsigneeID + "客户权限!");
                }
                if (OutboundValidationDA.UpdateOutboundValidation(dataBase, tran, outboundValidationInfo, whLoginID) > 0)
                {
                    strOutboundValidationID = outboundValidationInfo.ValidationID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strOutboundValidationID;
        }
    }
}

